package com.tao.utils;

import com.tao.constants.SystemConstant;
import com.tao.vo.TreeNodeVo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 季伟
 * 功能：
 */
public class TreeUtils {
    public static List<TreeNodeVo> buildTreeVo(List<TreeNodeVo> treeNodeList){
        return treeNodeList.stream()
                //最上层父id设置成0
                .filter(t -> t.getParentId().equals(SystemConstant.TOP_MENU_PARENT_ID))
                .peek(t->t.setChildren(getChildren(t,treeNodeList)))
                .collect(Collectors.toList());

    }
    //获取子树
    private static List<TreeNodeVo> getChildren(TreeNodeVo treeNode,List<TreeNodeVo> treeNodeList){
        return treeNodeList.stream()
                .filter(t -> t.getParentId().equals(treeNode.getId()))
                //递归调用，当流中没有数据时递归终止
                .peek(t->t.setChildren(getChildren(t,treeNodeList)))
                .collect(Collectors.toList());
    }
}
